package com.example.vlille2.parser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.example.vlille2.base.StationsBDD;

public class ParserService extends Service {

	private XMLReader xr;
	private StationHandler stationHandler;

	private static final int DELAI = 60000;
	public static final String SERVICE = "ParserService";
	public static final String ACTION_DATAUPDATE = "DataUpdate";

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		new Timer().schedule(new MyTimerTask(), 0);			

		return START_NOT_STICKY;
	}

	private class MyTimerTask extends TimerTask
	{
		@Override
		public void run() {

			//on teste si l'appli est encore en foreground ou pas
			ActivityManager activityManager = (ActivityManager)getSystemService( Context.ACTIVITY_SERVICE );
			List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
			boolean tourneEnFG = false;
			for(RunningAppProcessInfo appProcess : appProcesses){
				if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(getPackageName())){
					tourneEnFG = true;
					break;
				}
			}

			if(tourneEnFG) 
			{
				SAXParserFactory spf = SAXParserFactory.newInstance();
				SAXParser sp;
				Context contexte = getApplicationContext();
				try {
					sp = spf.newSAXParser();
					xr = sp.getXMLReader();

					//on parse la liste de toutes les stations
					xr.setContentHandler(new StationsHandler(contexte));
					URL url = new URL("http://vlille.fr/stations/xml-stations.aspx");
					xr.parse(new InputSource(url.openStream()));

					//on parse toutes les stations
					stationHandler = new StationHandler(contexte);
					xr.setContentHandler(stationHandler);

					StationsBDD bdd = StationsBDD.getInstance();
					bdd.open();
					List<Integer> listeIDs = bdd.getIDs();
					bdd.close();

					for(Integer unID : listeIDs)
					{
						Log.e("parser","mise a jour de la station "+unID);
						stationHandler.setIdStation(unID);
						try {
							url = new URL("http://vlille.fr/stations/xml-station.aspx?borne="+unID);
							xr.parse(new InputSource(url.openStream()));
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (SAXException e) {
							e.printStackTrace();
						}				
					}
					sendBroadcast(new Intent(ParserService.ACTION_DATAUPDATE));


				} catch (ParserConfigurationException e1) {
					e1.printStackTrace();
				} catch (SAXException e1) {
					e1.printStackTrace();
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			new Timer().schedule(new MyTimerTask(), DELAI);
		}
	}

	@Override
	public void onDestroy() {
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}	

}